﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTFF.WPF.CP;
using iTunesLib;

namespace iTFF.WPF.Model.Lyrics.LocalProviders
{
    class id3LocalProvider : ILyricsLocalProvider
    {

        #region Fields

        iTunesControlPanel _itcp;

        #endregion

        #region Private helpers

        public id3LocalProvider()
        {
            _itcp = ControlPanel.Instance.iTunesControlPanel;
        }

        #endregion

        #region ILyricsLocalProvider Members

        public string DisplayName
        {
            get { return "id3v2 MP3 Files"; }
        }

        /// <summary>
        /// Gets the lyrics using iTunes interface
        /// </summary>
        /// <param name="persistentID"></param>
        /// <returns></returns>
        public LyricsModel GetLyrics(ulong persistentID)
        {
            var track = _itcp.GetTrackFromPersistentID(persistentID);

            if (track != null)
            {
                var fileTrack = track as IITFileOrCDTrack;

                if (fileTrack != null && !string.IsNullOrEmpty(fileTrack.Lyrics))
                {
                    return new LyricsModel
                    {
                        Album = fileTrack.Album,
                        Artist = fileTrack.Artist,
                        iTunesPersistentID = _itcp.GetObjectPersistentID(fileTrack),
                        Lyrics = fileTrack.Lyrics,
                        Name = fileTrack.Name
                    };
                }
            }

            return null;
        }

        /// <summary>
        /// Saves the lyrics using iTunes interface
        /// </summary>
        /// <param name="persistentID"></param>
        /// <param name="force"></param>
        /// <param name="lyrics"></param>
        /// <returns></returns>
        public bool SaveLyrics(LyricsModel model, bool force)
        {

            var track = _itcp.GetTrackFromPersistentID(model.iTunesPersistentID);

            if (track != null)
            {
                var fileTrack = track as IITFileOrCDTrack;

                if (fileTrack != null)
                {
                    if (force || (!force && string.IsNullOrEmpty(fileTrack.Lyrics)))
                    {
                        fileTrack.Lyrics = model.Lyrics;
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Removes the lyrics using the iTunes interface
        /// </summary>
        /// <param name="persistentID"></param>
        /// <returns></returns>
        public bool RemoveLyrics(ulong persistentID)
        {
            var track = _itcp.GetTrackFromPersistentID(persistentID);

            if (track != null)
            {
                var fileTrack = track as IITFileOrCDTrack;

                if (fileTrack != null)
                {
                    fileTrack.Lyrics = string.Empty;
                    return true;
                }
            }

            return false;
        }

        public bool LyricsExists(ulong persistentID)
        {
            var track = _itcp.GetTrackFromPersistentID(persistentID);

            if (track != null)
            {
                var fileTrack = track as IITFileOrCDTrack;

                if (fileTrack != null)
                    return !string.IsNullOrEmpty(fileTrack.Lyrics);
            }

            return false;
        }

        public int Metric
        {
            get
            {
                return 20;
            }
        }

        public bool IsValid
        {
            get
            {
                return true;
            }
        }

        public bool IsActivated { get; set; }

        #endregion
    }
}
